<template>
  <el-container @keyup.enter.native="showTableData()">
    <el-header>
      <div class="head">
        <el-row>
          <el-col :span="6"
            ><div class="head_col">
              <el-autocomplete
                style="width: 100%"
                v-model="nodeName"
                size="small"
                :fetch-suggestions="querySearch"
                placeholder="输入节点名或者数据表名"
                :trigger-on-focus="false"
                @select="handleSelect"
              ></el-autocomplete></div
          ></el-col>
          <el-col :span="2">
            <el-select v-model="queryType" size="small">
              <el-option
                v-for="item in queryTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="10">
            <div class="but">
              <el-button size="small" type="primary" @click="queryTemplet"
                >查询</el-button
              >
            </div>
          </el-col>
          <el-col :span="6">
            <div class="father">
              <div class="son">
                <span style="font-size: 20px" type="success">
                  {{ title }}
                </span>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-header>
    <el-container>
      <el-aside width="380px">
        <div class="aside">
          <el-table
            :data="left_tableData"
            border
            stripe
            :height="clientHeight()"
            style="width: 100%"
          >
            <el-table-column
              prop="BILL_TEMPLETCAPTION"
              label="单据模板"
              width="179"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="NAME"
              label="节点名"
              show-overflow-tooltip
              width="100"
            >
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type=""
                  @click="queryComponent(scope.row.METADATACLASS)"
                  size="mini"
                  >查看</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-aside>
      <el-main>
        <el-row>
          <el-col :span="2"
            ><el-tag type="success" effect="plain">元数据组件</el-tag></el-col
          >
          <el-col :span="5">
            <el-select
              v-model="componentID"
              @change="queryClassse"
              placeholder="请选择"
              size="small"
            >
              <el-option
                v-for="item in components"
                :key="item.ID"
                :label="item.DISPLAYNAME"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="2"><el-tag effect="plain">CLASS对象</el-tag></el-col>
          <el-col :span="15">
            <el-select
              v-model="classID"
              @change="queryPropertys"
              style="width: 100%"
              placeholder="请选择"
              size="small"
            >
              <el-option
                v-for="item in classes"
                :key="item.ID"
                :label="item.TITLE"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <div class="main_table">
          <el-table
            stripe
            :height="clientHeight2()"
            :data="classPropertys"
            border
            style="width: 100%"
          >
            <el-table-column
              prop="DISPLAYNAME"
              label="列显示名"
              show-overflow-tooltip
              sortable
              width="200"
            >
            </el-table-column>
            <el-table-column label="数据类型" show-overflow-tooltip width="200">
              <template slot-scope="scope">
                <span v-if="scope.row.TYPE == '3'">{{
                  scope.row.TYPENAME
                }}</span>
                <el-link
                  @click="queryPropertyCloumns(scope.row.CID, scope.row.TYPE)"
                  v-if="scope.row.TYPE != '3'"
                  type="primary"
                  >{{ scope.row.TYPENAME }}</el-link
                >
              </template>
            </el-table-column>
            <el-table-column prop="NAME" sortable label="数据库列名">
            </el-table-column>
            <el-table-column
              prop="ATTRLENGTH"
              show-overflow-tooltip
              label="长度"
            >
            </el-table-column>
            <el-table-column
              prop="NULLABLE"
              show-overflow-tooltip
              label="是否可为空"
            >
            </el-table-column>
          </el-table>
        </div>
      </el-main>
    </el-container>
    <!-- 弹框 1 开始 -->
    <el-dialog title="枚举" :visible.sync="dialogVisible1" width="30%" center>
      <el-table :data="enum_tableData" border style="width: 100%">
        <el-table-column prop="NAME" label="枚举名"> </el-table-column>
        <el-table-column prop="VALUE" label="枚举值"> </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
    <!-- 弹框 1 结束 -->

    <!-- 弹框 2 开始 -->
    <el-dialog
      :title="refTitle"
      :visible.sync="dialogVisible2"
      top="5vh"
      width="60%"
      @close="dlgClose()"
      center
    >
      <el-table
        stripe
        :height="clientHeight3()"
        :data="classPropertys2"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="DISPLAYNAME"
          label="列显示名"
          show-overflow-tooltip
          sortable
          width="200"
        >
        </el-table-column>
        <el-table-column label="数据类型" show-overflow-tooltip width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.TYPE == '3'">{{ scope.row.TYPENAME }}</span>
            <el-link
              @click="queryPropertyCloumns(scope.row.CID, scope.row.TYPE)"
              v-if="scope.row.TYPE != '3'"
              type="primary"
              >{{ scope.row.TYPENAME }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="NAME" sortable label="数据库列名">
        </el-table-column>
        <el-table-column prop="ATTRLENGTH" show-overflow-tooltip label="长度">
        </el-table-column>
        <el-table-column
          prop="NULLABLE"
          show-overflow-tooltip
          label="是否可为空"
        >
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
    <!-- 弹框 2 结束 -->
    <el-input type="hidden" @keyup.enter.native="showTableData()"></el-input>
    <el-input
      type="hidden"
      
      >
    </el-input>
    <!-- 弹框 3 开始 -->
    <el-dialog
      :title="refTitle2"
      :visible.sync="dialogVisible3"
      top="5vh"
      width="80%"
      center
      @close="dlgClose2"
    >
      <el-table
        size="mini"
        stripe
        :height="clientHeight3()"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          v-for="(item, index, key) in columns"
          :key="key"
          :label="item.label"
          width="180"
        >
          <el-table-column
            :prop="item.prop"
            width="180"
            sortable
            :label="item.label2"
            show-overflow-tooltip
          ></el-table-column>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[50, 100, 200, 300, 400]"
        :page-size="pageSize"
        :pager-count="11"
        layout="total, sizes, prev, pager, next"
        :total="total">
      </el-pagination>
    </el-dialog>
    <!-- 弹框 3 结束 -->
  </el-container>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      queryType: "1",
      queryTypes: [
        {
          value: "1",
          label: "节点名",
        },
        {
          value: "2",
          label: "表名",
        },
      ],
      refTitle: "",
      enum_tableData: [],
      dialogVisible1: false,
      dialogVisible2: false,
      dialogVisible3: false,
      left_tableData: [],
      nodeName: "",
      components: [],
      componentID: "",
      classes: [],
      classID: "",
      classPropertys: [],
      title: "",
      classPropertys2: [],
      tableNams: [],
      tableData: [],
      currentPage: 1,
      pageSize: 50,
      columns: [],
      queryTableName: '',
      queryTableName2: '',
      total: 0,
      refTitle2: '',
      refTitle3: ''
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    dlgClose(){
      this.queryTableName2 = '';
      this.refTitle2 = this.refTitle3;
    },
    dlgClose2(){
      this.currentPage = 1;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.queryTableData('query');
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryTableData('page');
    },
    showTableData(){
      this.queryTableData('query');
      if(this.queryTableName != '' || this.queryTableName2 != ''){
        this.dialogVisible3 = true;
      }
    },
    buildCP(columns, cp){
      var objs = [];
      for(var i = 0; i < columns.length; i++){
        var obj = columns[i];
        for(var a = 0; a < cp.length; a++){
          if(columns[i].label == cp[a].NAME){
            obj.label2 = cp[a].DISPLAYNAME;
            break;
          }
        }
        objs.push(obj);
      }
      return objs;
    },
    queryTableData(type){
      this.tableData = [];
      this.columns=  [];
      var tn = '';
      if(type == 'query'){
        this.total = 0;
      }
      if(this.queryTableName != '' || this.queryTableName2 != ''){
        var cp = [];
        if(this.queryTableName2 != ''){
          tn = this.queryTableName2;
          cp = this.classPropertys2
        }else if(this.queryTableName != ''){
          tn = this.queryTableName;
          cp = this.classPropertys;
        }
        this.$http
          .get("/start/query/queryTableData", {
            params: {
              tableName: tn,
              pageSize: this.pageSize,
              currentPage: this.currentPage
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.tableData = data.data;
              this.columns = this.buildCP(data.columns, cp);
              if(type == 'query'){
                this.total = data.total;
              }
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
      }
    },
    querySearch(queryString, cb) {
      this.tableNams = [];
      if (this.queryType == "2") {
        this.$http
          .get("/start/query/queryTables", {
            params: {
              tableName: queryString,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.tableNams = data.data;
              // 调用 callback 返回建议列表的数据
              cb(this.tableNams);
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
      }else{
        cb(this.tableNams);
      }
    },
    handleSelect() {},
    queryPropertyCloumns(id, type) {
      if (type == "1") {
        this.enum_tableData = [];
        this.$http
          .get("/start/query/queryEnums", {
            params: {
              id: id,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.enum_tableData = data.data;
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
        this.dialogVisible1 = true;
      } else if (type == "2") {
        this.classPropertys2 = [];
        this.refTitle = "";
        this.$http
          .get("/start/query/queryClass", {
            params: {
              classId: id,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.refTitle =
                data.data.DISPLAYNAME +
                " / " +
                data.data.FULLCLASSNAME +
                " / " +
                data.data.DEFAULTTABLENAME;
              this.queryTableName2 = data.data.DEFAULTTABLENAME;
              this.refTitle2 = this.refTitle + " 表格数据";
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
        this.$http
          .get("/start/query/queryPropertys", {
            params: {
              classId: id,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.classPropertys2 = data.data;
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
        this.dialogVisible2 = true;
      }
    },
    queryPropertys() {
      this.clearData(4);
      this.$http
        .get("/start/query/queryClass", {
          params: {
            classId: this.classID,
          },
        })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.queryTableName = data.data.DEFAULTTABLENAME;
            this.refTitle2 = data.data.DISPLAYNAME +
                " / " +
                data.data.FULLCLASSNAME +
                " / " +
                data.data.DEFAULTTABLENAME + " 表格数据";
            this.refTitle3 = this.refTitle2;
          } else {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        });
      this.$http
        .get("/start/query/queryPropertys", {
          params: {
            classId: this.classID,
          },
        })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.classPropertys = data.data;
          } else {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        });
    },
    clearData(level) {
      if (level == 1) {
        this.left_tableData = [];
        this.components = [];
        this.componentID = "";
        this.classID = "";
        this.classes = [];
        this.classPropertys = [];
        this.queryTableName = '';
      } else if (level == 2) {
        this.componentID = "";
        this.components = [];
        this.classID = "";
        this.classes = [];
        this.classPropertys = [];
        this.queryTableName = '';
      } else if (level == 3) {
        this.classID = "";
        this.classes = [];
        this.classPropertys = [];
        this.queryTableName = '';
      } else if (level == 4) {
        this.classPropertys = [];
        this.queryTableName = '';
      }
    },
    queryClassse() {
      this.clearData(3);
      this.$http
        .get("/start/query/queryClasses", {
          params: {
            componentId: this.componentID,
          },
        })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.classes = data.data;
          } else {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        });
    },
    queryComponent(dataClass) {
      this.clearData(2);
      this.$http
        .get("/start/query/queryComponent", {
          params: {
            dataClass: dataClass,
          },
        })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.components = data.data;
          } else {
            this.$message({
              message: data.msg,
              type: "error",
            });
          }
        });
    },
    queryTemplet() {
      this.clearData(1);
      // 清空
      if (this.queryType == "1") {
        // 节点
        this.$http
          .get("/start/query/queryTemplate", {
            params: {
              nodeName: this.nodeName,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.left_tableData = data.data;
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
      } else {
        // 表名
        this.$http
          .get("/start/query/queryClassesByTabName", {
            params: {
              tabName: this.nodeName,
            },
          })
          .then(({ data }) => {
            if (data && data.code === 0) {
              this.classes = data.data;
            } else {
              this.$message({
                message: data.msg,
                type: "error",
              });
            }
          });
      }
    },
    clientHeight() {
      return this.$store.state.clientHeight - (60 + 10);
    },
    clientHeight2() {
      return this.$store.state.clientHeight - (120 + 10);
    },
    clientHeight3() {
      return this.$store.state.clientHeight - (220 + 10);
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    let clientHeight = document.documentElement.clientHeight;
    this.$store.state.clientHeight = clientHeight;
    this.$http
      .get("/start/query/getTitle", {
        params: {},
      })
      .then(({ data }) => {
        if (data && data.code === 0) {
          this.title = data.data;
        } else {
          this.$message({
            message: data.msg,
            type: "error",
          });
        }
      });
    const map = { 13: false};
    // 当前页面监视键盘输入
    document.onkeydown = (e) => {
      if (e.keyCode in map) {
        map[e.keyCode] = true;
        if (map[13]) {
          this.showTableData();
        }
      }
    };

    document.onkeyup = (e) => {
      if (e.keyCode in map) {
        map[e.keyCode] = false;
      }
    };
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
.head {
  padding-top: 10px;
}
.head_col {
  padding-right: 10px;
}
.aside {
  padding-left: 20px;
}
.el-main {
  padding-top: 0px;
}
.main_table {
  padding-top: 15px;
}
.father {
  width: 100%;
}
.son {
  margin-left: auto; //左对齐
  width: 300px;
  text-align: right;
}
.but {
  padding-left: 10px;
}
</style>